//
//  BaseTableViewCell.m
//  FlowLayout
//
//  Created by Fuzzie Liu on 15/10/29.
//  Copyright © 2015年 Fuzzie Liu. All rights reserved.
//

#import "BaseTableViewCell.h"

@implementation BaseTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_titleLabel];
        
        self.textField = [[UITextField alloc] init];
        [self addSubview:_textField];
        self.textField.font = [UIFont boldSystemFontOfSize:20];
        self.textField.borderStyle = UITextBorderStyleRoundedRect;
        // 去掉选择效果
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat width = self.contentView.bounds.size.width;
    CGFloat height = self.contentView.bounds.size.height;
    self.titleLabel.frame = CGRectMake(10, 5, (width - 30) / 8, height - 10);
    self.textField.frame = CGRectMake((width - 30) / 8 + 20, 5, (width - 30) * 7 / 8, height - 10);
}

+ (UIFont *)defaultFont
{
    return [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
}

+ (CGSize)sizeForContentString:(NSString *)string
{
    CGSize maxSize = CGSizeMake(336, 1000);
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineBreakMode:NSLineBreakByCharWrapping];
    NSDictionary *attributes = @{NSFontAttributeName : [self defaultFont], NSParagraphStyleAttributeName : style,};
    CGRect rect = [string boundingRectWithSize:maxSize options:options attributes:attributes context:nil];
    return rect.size;
}

@end
